package com.itheima.web.servlet;


import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.service.BrandService2;
import com.itheima.service.impl.BrandServiceImpl;
import com.itheima.service.impl.BrandServiceImpl2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand2/*")
public class BrandServlet2 extends BaseServlet{
    private BrandService2 brandService2 = new BrandServiceImpl2();

    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 调用service查询
        List<Brand> brands = brandService2.selectAll();

        //2. 转为JSON
        String jsonString = JSON.toJSONString(brands);
        //3. 写数据
        //response.setContentType("text/json;charset=UTF-8");
        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(jsonString);
    }
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       BufferedReader br=request.getReader();
       String params=br.readLine();

       Brand brand=JSON.parseObject(params,Brand.class);

       brandService2.add(brand);

       response.getWriter().write("success");
    }
    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader br=request.getReader();
        String params=br.readLine();
        // System.out.println(params);  一个数组[11,12]


        int[] ids=JSON.parseObject(params,int[].class);

        brandService2.deleteByIds(ids);

        response.getWriter().write("success");
    }
    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 接收 当前页码 和 每页展示条数    url?currentPage=1&pageSize=5
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

       PageBean<Brand> pageBean=brandService2.selectByPage(currentPage,pageSize);

       String jsonString=JSON.toJSONString(pageBean);

       response.setContentType("text/json;charset=UTF-8");
       response.getWriter().write(jsonString);
    }
    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 接收 当前页码 和 每页展示条数    url?currentPage=1&pageSize=5
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        BufferedReader br=request.getReader();
        String params=br.readLine();

        Brand brand=JSON.parseObject(params,Brand.class);

        PageBean<Brand> pageBean=brandService2.selectByPageAndCondition(currentPage,pageSize,brand);

        String jsonString=JSON.toJSONString(pageBean);

        response.setContentType("text/json;charset=UTF-8");
        response.getWriter().write(jsonString);
    }
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader br=request.getReader();
        String params=br.readLine();

        Brand brand=JSON.parseObject(params,Brand.class);

        brandService2.update(brand);

        response.getWriter().write("success");
    }
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //get写法
//        String _id=request.getParameter("id");
//
//        int id=Integer.parseInt(_id);
        //post写法

        BufferedReader br=request.getReader();
        String params=br.readLine();
        int id=Integer.parseInt(params);

        //System.out.println(params);

        brandService2.delete(id);

        response.getWriter().write("success");
    }







}
